package com.telenav.snowsweeper.renderer;

import com.telenav.snowsweeper.data.Edge;
import com.telenav.snowsweeper.data.EdgeMap;
import com.telenav.snowsweeper.data.Point;
import com.telenav.snowsweeper.util.DisplayUtil;

import android.graphics.Rect;

public class MapEdgeBuilder {
	private EdgeMap edgeMap;
	
	public MapEdgeBuilder(EdgeMap edgeMap) {
		this.edgeMap = edgeMap;
	}
	
	public void build(MapEdgeRenderer.RendererParams params, long timeStamp) {
		params.edgesOnScreen.clear();
		Rect clip = getClip(params);
		edgeMap.clipEdges(clip, params.edgesOnScreen);
		
		Point pnt;
		for (Edge edge : params.edgesOnScreen) {
			if (edge == null)
				continue;
			pnt = edge.getBegin();
			if (pnt.getScreenXY(timeStamp) == null) {
				Util.mapPoint2ScreenPoint(pnt, clip, params.scaleX, params.scaleY, timeStamp);
			}
			pnt = edge.getEnd();
			if (pnt.getScreenXY(timeStamp) == null) {
				Util.mapPoint2ScreenPoint(pnt, clip, params.scaleX, params.scaleY, timeStamp);
			}
		}
		params.timeStamp = timeStamp;
	}
	
	public Rect getClip(MapEdgeRenderer.RendererParams params) {
		Rect r = new Rect();
		int spanX = Util.screenDist2Map(Util.canvasWidth, params.scaleX) + 1;
		int spanY = Util.screenDist2Map(Util.canvasHeight, params.scaleY) + 1;
		r.left = params.centerX - (spanX >> 1);
		r.right = r.left + spanX;
		r.top = params.centerY - (spanY >> 1);
		r.bottom = r.top + spanY;
		return r;
	}
}
